home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus Special 25
/
AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso
/
Updates
/
PowerPC
/
pdflib
/
test
/
pdftest.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-05-16
|
25KB
|
988 lines
/*---------------------------------------------------------------------------*
| PDFlib - A library for dynamically generating PDF files |
+---------------------------------------------------------------------------+
| Copyright (c) 1997-1999 Thomas Merz. All rights reserved. |
+---------------------------------------------------------------------------+
| This software is not in the public domain. It is subject to the |
| "Aladdin Free Public License". See the file license.txt for details. |
| This license grants you the right to use and redistribute PDFlib |
| under certain conditions. Among other things, the license requires |
| that the copyright notice and this notice be preserved on all copies. |
| This requirement extends to ports to other programming languages. |
| |
| In short, you are allowed to develop and use PDFlib-based software |
| as long as you don't sell it. Commercial use of PDFlib requires a |
| commercial license which can be obtained from the author of PDFlib. |
| Contact information can be found in the accompanying PDFlib manual. |
| PDFlib is distributed with no warranty of any kind. Commercial users, |
| however, will receive warranty and support statements in writing. |
*---------------------------------------------------------------------------*/
/* pdftest.c
*
* Test bed and sample application for PDFlib
*/
#ifdef WIN32
/* Visual C++ seems to need this in order to fetch the thread functions */
#ifndef _MT
#define _MT
#endif
#include <windows.h>
#include <process.h>
#endif /* WIN32 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "pdflib.h"
#if __POWERPC__ || __CFM68K__ || __MC68K__
#define MAC
#endif
/* Don't clutter output from many threads with messages */
#ifdef WIN32
#define MESSAGE(m) /* */
#define PDF_ENDTHREAD(val) _endthreadex(val);
#else
#define MESSAGE(m) fprintf(stderr, m)
#define PDF_ENDTHREAD(val) /* */
#endif
/* ------------------------------------------------------------- */
static void
bookmarks(PDF *p)
{
#define UNICODEINFO "\xFE\xFF\0001\000.\000A\0\0"
#define LEFT 50
#define FONTSIZE 24
#define LEAD ((int) (FONTSIZE * 1.5))
unsigned char buf[64], tmp[64];
int c, i, pos;
float y=700;
int level1, level2=0, level3=0, font;
MESSAGE("Bookmark test...");
font = PDF_findfont(p, "Helvetica", "default", 0);
if (font == -1) {
fprintf(stderr, "\nFont %s not found!\n", "Helvetica");
return;
}
PDF_begin_page(p, a4_width, a4_height);
PDF_setfont(p, font, FONTSIZE);
PDF_show_xy(p, "The bookmarks for this page contain all", LEFT, y-=LEAD);
PDF_show_xy(p, "Unicode characters.", LEFT, y-=LEAD);
PDF_show_xy(p, "Depending on the available fonts,", LEFT, y-=LEAD);
PDF_show_xy(p, "only a certain subset will be visible on", LEFT, y-=LEAD);
PDF_show_xy(p, "your system.", LEFT, y-=LEAD);
/* private Unicode info entry */
PDF_set_info(p, "Revision", UNICODEINFO);
/* Generate Unicode bookmarks */
level1 = PDF_add_bookmark(p, "Unicode bookmarks", 0, 0);
for (c = 0; c < 65535; c += 16) {
if (c % 4096 == 0) {
sprintf((char *) tmp, "U+%04X", c);
level2 = PDF_add_bookmark(p, (char *) tmp, level1, 0);
}
if (c % 256 == 0) {
sprintf((char *) tmp, "U+%04X", c);
level3 = PDF_add_bookmark(p, (char *) tmp, level2, 0);
}
/* now comes the actual Unicode string with the BOM */
sprintf((char *) tmp, "0x%04X: ", c);
/* write the Unicode byte order mark */
strcpy((char *) buf, "\376\377");
for (pos = 0; tmp[pos/2]; /* */ ) {
buf[2+pos++] = 0;
buf[2+pos++] = tmp[pos/2]; /* construct Unicode string */
}
pos += 2; /* account for the BOM */
for (i = 0; i < 16; i++) { /* continue filling buf with chars */
buf[pos++] = (unsigned char) ((((c+i)) & 0xFF00) >> 8);
buf[pos++] = (unsigned char) (((c+i)) & 0x00FF);
}
/* signal end of string with two null bytes */
buf[pos++] = (unsigned char) 0;
buf[pos++] = (unsigned char) 0;
(void) PDF_add_bookmark(p, (char *) buf, level3, 1);
}
PDF_end_page(p);
MESSAGE("done\n");
#undef FONTSIZE
#undef LEFT
#undef UNICODEINFO
#undef LEAD
}
/* ------------------------------------------------------------- */
static void
ccitt_image(PDF *p)
{
int image;
float sx, sy;
#define CCITTFILE "tm.g3"
#define WIDTH 591
#define HEIGHT 236
/* Scale to non-standard fax resolutions */
#define FAX_DPI_X 72
#define FAX_DPI_Y 72
sx = (float) (72.0 / FAX_DPI_X);
sy = (float) (72.0 / FAX_DPI_Y);
MESSAGE("CCITT test...");
if ((image = PDF_open_CCITT(p, CCITTFILE, WIDTH, HEIGHT, 0, 0, 0)) == -1) {
fprintf(stderr, "Error: Couldn't open CCITT image %s.\n", CCITTFILE);
return;
}
PDF_begin_page(p, (float) PDF_get_image_width(p, image) * sx,
(float) PDF_get_image_height(p, image) * sy);
(void) PDF_add_bookmark(p, "CCITT image", 0, 1);
PDF_scale(p, sx, sy);
PDF_place_image(p, image, (float) 0.0, (float) 0.0, (float) 1.0);
PDF_end_page(p);
MESSAGE("done\n");
#undef CCITTFILE
}
/* ------------------------------------------------------------- */
static void
gif_image(PDF *p)
{
int image;
#define GIFFILE "pdflib.gif"
MESSAGE("GIF test...");
if ((image = PDF_open_GIF(p, GIFFILE)) == -1) {
fprintf(stderr, "Error: Couldn't analyze GIF image %s.\n", GIFFILE);
return;
}
PDF_begin_page(p, (float) PDF_get_image_width(p, image),
(float) PDF_get_image_height(p, image));
(void) PDF_add_bookmark(p, "GIF image", 0, 1);
PDF_place_image(p, image, (float) 0.0, (float) 0.0, (float) 1.0);
PDF_end_page(p);
MESSAGE("done\n");
#undef GIFFILE
}
/* ------------------------------------------------------------- */
#ifdef HAVE_LIBTIFF
static void
tiff_image(PDF *p)
{
int image;
#define TIFFFILE "acroweb_j.tif"
MESSAGE("TIFF test...");
if ((image = PDF_open_TIFF(p, TIFFFILE)) == -1) {
fprintf(stderr, "Error: Couldn't analyze TIFF image %s.\n", TIFFFILE);
return;
}
PDF_begin_page(p, PDF_get_image_width(p, image), PDF_get_image_height(p, image));
(void) PDF_add_bookmark(p, "TIFF image", 0, 1);
PDF_place_image(p, image, 0.0, 0.0, 1.0);
PDF_end_page(p);
MESSAGE("done\n");
#undef TIFFFILE
}
#endif
/* ------------------------------------------------------------- */
static void
jpeg_image(PDF *p)
{
int image;
float scale;
#define JPEGFILE "nesrin.jpg"
MESSAGE("JPEG test...");
if ((image = PDF_open_JPEG(p, JPEGFILE)) == -1) {
fprintf(stderr, "Error: Couldn't analyze JPEG image %s.\n", JPEGFILE);
return;
}
PDF_begin_page(p, a4_width, a4_height);
(void) PDF_add_bookmark(p, "JPEG image", 0, 1);
/* ----------------- first image ------------------- */
/* fit image to page width */
scale = (float) a4_width/PDF_get_image_width(p, image);
PDF_place_image(p, image, (float) 0.0,
a4_height - PDF_get_image_height(p, image) * scale, scale);
/* ----------------- second image ------------------- */
scale = (float) 0.5;
PDF_save(p);
PDF_rotate(p, (float) 90.0);
PDF_place_image(p, image, (float) 0, (float) (-1 * a4_width), scale);
PDF_restore(p);
/* ----------------- third image ------------------- */
scale = (float) 0.25;
PDF_save(p);
PDF_rotate(p, (float) 45.0);
PDF_place_image(p, image, (float) 200, (float) 0.0, scale);
PDF_restore(p);
PDF_end_page(p);
MESSAGE("done\n");
#undef JPEGFILE
}
/* ------------------------------------------------------------- */
static void
character_table(PDF *p)
{
char text[50];
int i, j, font;
float x, y;
#define LEFT 50
#define TOP 700
#define FONTSIZE 16
#define FONTNAME "Times-Roman"
#ifdef MAC
#define ENCODING "macroman"
#else
#define ENCODING "winansi"
#endif
MESSAGE("Character encoding test...");
font = PDF_findfont(p, FONTNAME, ENCODING, 1);
if (font == -1) {
fprintf(stderr, "Fon